package com.psds.quartz;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * author: cjianquan
 * date: 2016/8/30
 */
@Service("quartzService")
public class QuartzService {

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    public List<QuartzEntity> getQuartzList(){
        QuartzEntityMapper mp = this.sqlSessionTemplate.getMapper(QuartzEntityMapper.class);
        QuartzEntityExample qe = new QuartzEntityExample();
        qe.createCriteria().andQuartzEnableEqualTo("1");
        List<QuartzEntity> list =  mp.selectByExample(qe);
        return list;
    }


    public int queryQuartzCount(QuartzEntity quartz){
        QuartzEntityMapper mp = this.sqlSessionTemplate.getMapper(QuartzEntityMapper.class);
        QuartzEntityExample qe = new QuartzEntityExample();
        QuartzEntityExample.Criteria criteria = qe.createCriteria();
        if(quartz.getQuartzApp()!=null && !"".equals(quartz.getQuartzApp())){
            criteria.andQuartzAppLike("%"+quartz.getQuartzApp()+"%");
        }
        if(quartz.getQuartzJobname()!=null && !"".equals(quartz.getQuartzJobname())){
            criteria.andQuartzJobnameLike("%" + quartz.getQuartzJobname() + "%");
        }
        if(quartz.getQuartzJobnameCn()!=null && !"".equals(quartz.getQuartzJobnameCn())){
            criteria.andQuartzJobnameCnLike("%"+quartz.getQuartzJobnameCn()+"%");
        }
        int cnt = mp.countByExample(qe);
        return cnt;
    }

    public List<QuartzEntity> queryQuartzList(QuartzEntity quartz,int start,int offset){
        QuartzEntityMapper mp = this.sqlSessionTemplate.getMapper(QuartzEntityMapper.class);
        QuartzEntityExample qe = new QuartzEntityExample();
        QuartzEntityExample.Criteria criteria = qe.createCriteria();
        if(quartz.getQuartzApp()!=null && !"".equals(quartz.getQuartzApp())){
            criteria.andQuartzAppLike("%"+quartz.getQuartzApp()+"%");
        }
        if(quartz.getQuartzJobname()!=null && !"".equals(quartz.getQuartzJobname())){
            criteria.andQuartzJobnameLike("%"+quartz.getQuartzJobname() + "%");
        }
        if(quartz.getQuartzJobnameCn()!=null && !"".equals(quartz.getQuartzJobnameCn())){
            criteria.andQuartzJobnameCnLike("%"+quartz.getQuartzJobnameCn()+"%");
        }
        qe.setStart(start);
        qe.setEnd(offset);
        return mp.selectByExample(qe);
    }

    public QuartzEntity getQuartzById(String id){
        QuartzEntityMapper mp = this.sqlSessionTemplate.getMapper(QuartzEntityMapper.class);
        return mp.selectByPrimaryKey(id);
    }

    public void addQuartz(QuartzEntity quartz){
        QuartzEntityMapper mp = this.sqlSessionTemplate.getMapper(QuartzEntityMapper.class);
        mp.insertSelective(quartz);
    }

    public void updateQuartz(QuartzEntity quartz){
        QuartzEntityMapper mp = this.sqlSessionTemplate.getMapper(QuartzEntityMapper.class);
        mp.updateByPrimaryKeySelective(quartz);
    }

    public void delQuartzById(String id){
        QuartzEntityMapper mp = this.sqlSessionTemplate.getMapper(QuartzEntityMapper.class);
        mp.deleteByPrimaryKey(id);
    }

    public String checkQuartz(QuartzEntity quartz){
        QuartzEntityMapper mp = this.sqlSessionTemplate.getMapper(QuartzEntityMapper.class);
        String msg = "";
        String id = quartz.getId();
        int cnt = -1;
        if(id==null || id.equals("")){
            QuartzEntityExample qe = new QuartzEntityExample();
            qe.createCriteria().andQuartzJobnameEqualTo(quartz.getQuartzJobname());
            cnt = mp.countByExample(qe);
            if(cnt>0){
                msg = "任务名称："+quartz.getQuartzJobname()+"已存在";
                return msg;
            }
        }else{
            QuartzEntityExample qe = new QuartzEntityExample();
            qe.createCriteria().andQuartzJobnameEqualTo(quartz.getQuartzJobname()).andIdNotEqualTo(id);
            cnt = mp.countByExample(qe);
            if(cnt>0){
                msg = "任务名称："+quartz.getQuartzJobname()+"已存在";
                return msg;
            }
        }
        return msg;
    }

}
